package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.PublicService;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.Validator;
import tt.dz.appserver.util.enums.Response;

/**
 * 公共业务控制类
 * 
 * @author xielf
 *
 */
@RestController
@RequestMapping("/pub")
public class PublicController {

	/** 业务接口 */
	@Resource(name = "publicService")
	private PublicService publicService;

	/**
	 * 发送验证码
	 * 
	 * @param phone
	 *            手机号码
	 * @param merchantCode
	 *            运营商
	 * @return
	 */
	@RequestMapping(value = "/sendVerifyCode", produces = { "application/json;charset=utf-8" },method=RequestMethod.GET)
	public String sendVerifyCode(@RequestParam String phone,@RequestParam(value="merchantCode",required=false,defaultValue="") String merchantCode) {

		// 返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		try {

			// 手机号码有误
//			if (!Validator.isMobile(phone)) {
//				resultMappingEntity = Result.responseBody(Response.MOBILE_ERROR.getCode(),
//						Response.MOBILE_ERROR.getMsg());
//				return resultMappingEntity.toString();
//			}
			if(merchantCode.equals("")){
				merchantCode=Constants.merchantCode;
			}
			JSONObject mainServerResult = publicService.sendVerifyCode(phone,merchantCode);

			String code = mainServerResult.getString("code");
			String msg = mainServerResult.getString("msg");
			// 成功
			if (Response.SUCCESS.getCode().equals(code)) {
				resultMappingEntity = Result.responseBody(code, msg, mainServerResult.getString("checkCode"));
			}
			// 失败
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
			// 异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(),
					e.getMessage());
		}
		return resultMappingEntity.toString();
	}

	/**
	 * 校验验证码
	 * 
	 * @param phone
	 *            手机号码
	 * @param verifyCode
	 *            验证码
	 * @param merchantCode
	 *            运营商
	 * @param source
	 * 			   渠道
	 * @param  account
	 * 			  渠道账户
	 *      
	 * @return
	 */
	@RequestMapping(value = "/validationVerifyCode", produces = {
			"application/json;charset=utf-8" }, method = RequestMethod.POST)
	public String validationVerifyCode(@RequestParam String phone,@RequestParam(value = "verifyCode") String verifyCode,
			@RequestParam(value = "source",required=false,defaultValue="") String source,
			@RequestParam(value = "merchantCode",required=false,defaultValue="") String merchantCode,
			@RequestParam(value = "account",required=false,defaultValue="") String account,
			@RequestParam(value = "type",required=false,defaultValue="") String type,
			@RequestParam(value = "bindingType",required=false,defaultValue="") String  bindingType) {

		ResultMappingEntity resultMappingEntity = null;
		try {

			// 手机号码有误
			JSONObject mainServerResult = publicService.validationVerifyCode(phone, verifyCode,source, merchantCode, account,type,bindingType);

			String code = mainServerResult.getString("code");
			String msg = mainServerResult.getString("msg");
			// 成功
			if (Response.SUCCESS.getCode().equals(code)) {
				resultMappingEntity = Result.responseBody(code, msg, mainServerResult.getString("checkCode"));
			}
			// 失败
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(),
					e.getMessage());
		}
		return resultMappingEntity.toString();
	}

	/**
	 * 检查更新
	 * @param osType 1android 2ios
	 * @return
	 */
	@RequestMapping(value = "/checkVersionUpdate", produces = {
			"application/json;charset=utf-8" }, method = RequestMethod.GET)
	public String checkVersionUpdate(@RequestParam(value = "osType") Integer osType) {

		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getLatestAppVersion(osType);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();

	}
	
	/**
	 * 服务点查询
	 * @param serviceType
	 * @param pageNo
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	@RequestMapping(value="/serviceStation", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String serviceStation(
			@RequestParam(defaultValue="1") int serviceType,
			@RequestParam(defaultValue="1") int pageNo,
			@RequestParam(required=true) float longitude,
			@RequestParam(required=true) float latitude){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getServiceStation(serviceType, pageNo, longitude, latitude);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 服务点详细
	 * @param serviceType
	 * @param pageNo
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	@RequestMapping(value="/serviceStationDetail", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String serviceStation(String serviceStationId){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getServiceStation(serviceStationId);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取用户
	 * @param source
	 * @param merchantCode
	 * @return
	 */
	@RequestMapping(value="/getAccessToken", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getAccessToken(String source,String merchantCode,String code){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getAccessToken(source,merchantCode,code);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/getThirdUserinfo", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getThirdUserinfo(String source,String merchantCode,String code){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getThirdUserinfo(source,merchantCode,code);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	@RequestMapping(value="/getJsSign", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getJsSign(String source,String merchantCode,String signType,String url){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getJsSign(source,merchantCode,signType,url);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 获取所有banner图片
	 * @return
	 */
	@RequestMapping(value="/bannerList", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String bannerList(){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getAllBannerList();
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/getLables", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getLables(Integer type,@RequestParam(required=false)Integer level){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getLables(type,level);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(), Response.FIAL.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/getCityNameAndCodeByCoordinate", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getCityNameAndCodeByCoordinate(String latitude,String longtitude){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getCityNameAndCodeByCoordinate(longtitude,latitude);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(), Response.FIAL.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/getAllOperator", produces = { "application/json;charset=utf-8" }, method=RequestMethod.GET)
	public String getAllOperator(){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = publicService.getAllOperator();
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(), Response.FIAL.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	

	@RequestMapping(value = "/test", produces = { "application/json;charset=utf-8" }, method = RequestMethod.GET)
	public String test() {
		return "{\"a\":1,\"b\":2}";
	}
}
